package com.slipper.common.util;

import java.io.UnsupportedEncodingException;

public class StringUtil {
    /**
     * 判断字符串是否为空
     */
    public static boolean isBlank(Object object) {
        return object == null
                || object.toString().trim().isEmpty()
                || "null".equals(object.toString().trim());
    }

    public static boolean isNotBlank(Object object) {
        return !isBlank(object);
    }
    
    public static byte[] toBytes(String s, String charset) throws UnsupportedEncodingException {
        if (s == null) {
            return new byte[] {};
        } else {
            return s.getBytes(charset);
        }
    }
    
    public static String concatPath(String path1, String path2) {
        if (path1 == null) {
            path1 = "";
        }
        if (path2 == null) {
            path2 = "";
        }
        if (path1.endsWith("/") && path2.startsWith("/")) {
            return path1.concat(path2.substring(1));
        } else if (path1.endsWith("/") || path2.startsWith("/")) {
            return path1.concat(path2);
        } else {
            return path1.concat("/").concat(path2);
        }            
    }

    public static String getLength(String str, int length) {
        if (isBlank(str) || str.length() < length) {
            return str;
        }
        return str.substring(0, length);
    }
}
